home *** CD-ROM | disk | FTP | other *** search
/ Express Pd: GALORE / Express Pd Galore - The Amiga PD & Shareware CD (1994)(Express Pd)[!][Amiga-CD32-CDTV].iso / productivity / term / termxem.c < prev    next >
C/C++ Source or Header  |  1993-07-16  |  8KB  |  465 lines

  1. /*
  2. **    termXEM.c
  3. **
  4. **    External emulation support routines
  5. **
  6. **    Copyright © 1990-1993 by Olaf `Olsen' Barthel & MXM
  7. **        All Rights Reserved
  8. */
  9.  
  10. #include "termGlobal.h"
  11.  
  12.     /* xem_swrite():
  13.      *
  14.      *    Send a few bytes across the serial line.
  15.      */
  16.  
  17. LONG __saveds __asm
  18. xem_swrite(register __a0 STRPTR Buffer,register __d0 LONG Size)
  19. {
  20.     if(WriteRequest)
  21.     {
  22.         SerWrite(Buffer,Size);
  23.  
  24.         return(0);
  25.     }
  26.     else
  27.         return(-1);
  28. }
  29.  
  30.     /* xem_sbreak():
  31.      *
  32.      *    Send a break signal across the serial line.
  33.      */
  34.  
  35. LONG __asm __saveds
  36. xem_sbreak(VOID)
  37. {
  38.     if(!WriteRequest)
  39.         return(-1);
  40.     else
  41.     {
  42.         WriteRequest -> IOSer . io_Command = SDCMD_BREAK;
  43.  
  44.         return((LONG)DoIO(WriteRequest));
  45.     }
  46. }
  47.  
  48.     /* xem_sstart():
  49.      *
  50.      *    Restart serial read activity.
  51.      */
  52.  
  53. VOID __asm __saveds
  54. xem_sstart(VOID)
  55. {
  56.     RestartSerial();
  57. }
  58.  
  59.     /* xem_sstop():
  60.      *
  61.      *    Stop serial read activity.
  62.      */
  63.  
  64. LONG __asm __saveds
  65. xem_sstop(VOID)
  66. {
  67.     if(ReadRequest)
  68.     {
  69.         if(!CheckIO(ReadRequest))
  70.             AbortIO(ReadRequest);
  71.  
  72.         WaitIO(ReadRequest);
  73.     }
  74.  
  75.     return(0);
  76. }
  77.  
  78.     /* xem_tgets(STRPTR Prompt,STRPTR Buffer,ULONG Size):
  79.      *
  80.      *    Get a string from the user.
  81.      */
  82.  
  83. LONG __saveds __asm
  84. xem_tgets(register __a0 STRPTR Prompt,register __a1 STRPTR Buffer,register __d0 ULONG Size)
  85. {
  86.     return(xpr_gets(Prompt,Buffer));
  87. }
  88.  
  89.     /* xem_tbeep(ULONG Times,ULONG Delay):
  90.      *
  91.      *    Beep the terminal display.
  92.      */
  93.  
  94. VOID __saveds __asm
  95. xem_tbeep(register __d0 ULONG Times,register __d1 ULONG Delay)
  96. {
  97.     WORD i;
  98.  
  99.     for(i = 0 ; i < Times ; i++)
  100.     {
  101.             /* Handle the visual part. */
  102.  
  103.         if(Config -> TerminalConfig -> BellMode != BELL_AUDIBLE)
  104.         {
  105.             if(StatusProcess)
  106.                 Signal(StatusProcess,SIG_BELL);
  107.         }
  108.  
  109.             /* Let it beep. */
  110.  
  111.         if(Config -> TerminalConfig -> BellMode == BELL_AUDIBLE || Config -> TerminalConfig -> BellMode == BELL_BOTH)
  112.             SoundPlay(SOUND_BELL);
  113.     }
  114. }
  115.  
  116.     /* xem_macrodispatch(struct XEmulatorMacroKey *XEM_MacroKey):
  117.      *
  118.      *    Dispatch a macro key call.
  119.      */
  120.  
  121. LONG __saveds __asm
  122. xem_macrodispatch(register __a0 struct XEmulatorMacroKey *XEM_MacroKey)
  123. {
  124.     VOID (*Routine)(VOID);
  125.  
  126.         /* If a routine to call is available (most likely xON or xOFF),
  127.          * make a call to it, else process the macro key data.
  128.          */
  129.  
  130.     if(Routine = (VPTR)XEM_MacroKey -> xmk_UserData)
  131.         (*Routine)();
  132.     else
  133.         SerialCommand(MacroKeys -> Keys[XEM_MacroKey -> xmk_Qualifier][XEM_MacroKey -> xmk_Code - 0x50]);
  134.  
  135.     return(0);
  136. }
  137.  
  138.     /* SetEmulatorOptions(BYTE Mode):
  139.      *
  140.      *    Save or load the emulator options.
  141.      */
  142.  
  143. BYTE
  144. SetEmulatorOptions(BYTE Mode)
  145. {
  146.     BYTE Success = FALSE;
  147.  
  148.         /* Is the library available and running? */
  149.  
  150.     if(XEmulatorBase && XEM_IO)
  151.     {
  152.             /* Are we using the new library code? */
  153.  
  154.         if(XEmulatorBase -> lib_Version >= 4)
  155.         {
  156.                 /* Get the name of the library. */
  157.  
  158.             strcpy(SharedBuffer,FilePart(XEmulatorBase -> lib_Node . ln_Name));
  159.  
  160.                 /* Does it have any name? */
  161.  
  162.             if(SharedBuffer[0])
  163.             {
  164.                 UBYTE    OtherBuffer[50];
  165.                 WORD    i;
  166.  
  167.                     /* Strip the `.library' bit. */
  168.  
  169.                 for(i = strlen(SharedBuffer) - 1 ; i >= 0 ; i--)
  170.                 {
  171.                     if(SharedBuffer[i] == '.')
  172.                     {
  173.                         SharedBuffer[i] = 0;
  174.  
  175.                         break;
  176.                     }
  177.                 }
  178.  
  179.                     /* What are we to do? */
  180.  
  181.                 if(Mode == XEM_PREFS_LOAD)
  182.                 {
  183.                         /* Restore settings... */
  184.  
  185.                     strcpy(OtherBuffer,"ENV:");
  186.  
  187.                     if(AddPart(OtherBuffer,SharedBuffer,50))
  188.                     {
  189.                             /* If we can't load them,
  190.                              * reset to defaults.
  191.                              */
  192.  
  193.                         if(!XEmulatorPreferences(XEM_IO,OtherBuffer,Mode))
  194.                         {
  195.                             strcpy(OtherBuffer,"ENV:xem");
  196.  
  197.                             if(AddPart(OtherBuffer,SharedBuffer,50))
  198.                             {
  199.                                 if(XEmulatorPreferences(XEM_IO,OtherBuffer,Mode))
  200.                                     Success = TRUE;
  201.                             }
  202.                         }
  203.                         else
  204.                             Success = TRUE;
  205.  
  206.                         if(!Success)
  207.                             XEmulatorPreferences(XEM_IO,NULL,XEM_PREFS_RESET);
  208.                     }
  209.                 }
  210.                 else
  211.                 {
  212.                         /* Save settings to ENV: */
  213.  
  214.                     strcpy(OtherBuffer,"ENV:");
  215.  
  216.                     if(AddPart(OtherBuffer,SharedBuffer,50))
  217.                     {
  218.                         if(XEmulatorPreferences(XEM_IO,OtherBuffer,Mode))
  219.                             Success = TRUE;
  220.                     }
  221.  
  222.                     if(Success)
  223.                     {
  224.                         Success = FALSE;
  225.  
  226.                             /* Save settings to ENVARC: */
  227.  
  228.                         strcpy(OtherBuffer,"ENVARC:");
  229.  
  230.                         if(AddPart(OtherBuffer,SharedBuffer,50))
  231.                         {
  232.                             if(XEmulatorPreferences(XEM_IO,OtherBuffer,Mode))
  233.                                 Success = TRUE;
  234.                         }
  235.                     }
  236.                 }
  237.             }
  238.         }
  239.     }
  240.  
  241.         /* Return result. */
  242.  
  243.     return(Success);
  244. }
  245.  
  246.     /* SetupEmulator(BYTE OpenConsole):
  247.      *
  248.      *    Initialize the XEM_IO structure.
  249.      */
  250.  
  251. STATIC BYTE
  252. SetupEmulator(VOID)
  253. {
  254.     if(!XEM_IO)
  255.     {
  256.         if(XEM_IO = (struct XEM_IO *)AllocVec(sizeof(struct XEM_IO),MEMF_ANY|MEMF_CLEAR))
  257.         {
  258.             XEM_IO -> xem_window        = Window;
  259.             XEM_IO -> xem_font        = CurrentFont;
  260.             XEM_IO -> xem_signal        = &XEM_Signal;
  261.             XEM_IO -> xem_screendepth    = Window -> WScreen -> RastPort . BitMap -> Depth;
  262.  
  263.             XEM_IO -> xem_sread        = xpr_sread;
  264.             XEM_IO -> xem_swrite        = xem_swrite;
  265.             XEM_IO -> xem_sflush        = xpr_sflush;
  266.             XEM_IO -> xem_sbreak        = xem_sbreak;
  267.             XEM_IO -> xem_squery        = xpr_squery;
  268.             XEM_IO -> xem_sstart        = xem_sstart;
  269.             XEM_IO -> xem_sstop        = xem_sstop;
  270.  
  271.             XEM_IO -> xem_tbeep        = xem_tbeep;
  272.             XEM_IO -> xem_tgets        = xem_tgets;
  273.             XEM_IO -> xem_toptions        = xpr_options;
  274.  
  275.             XEM_IO -> xem_process_macrokeys    = xem_macrodispatch;
  276.  
  277.             return(TRUE);
  278.         }
  279.     }
  280.     else
  281.         return(FALSE);
  282.  
  283.     return(FALSE);
  284. }
  285.  
  286.     /* CloseEmulator():
  287.      *
  288.      *    Close the emulation library.
  289.      */
  290.  
  291. VOID
  292. CloseEmulator()
  293. {
  294.     if(XEmulatorBase)
  295.     {
  296.         if(XEM_IO)
  297.         {
  298.             XEmulatorMacroKeyFilter(XEM_IO,NULL);
  299.             XEmulatorCloseConsole(XEM_IO);
  300.             XEmulatorCleanup(XEM_IO);
  301.  
  302.             FreeVec(XEM_IO);
  303.  
  304.             XEM_IO = NULL;
  305.         }
  306.  
  307.         CloseLibrary(XEmulatorBase);
  308.  
  309.         strcpy(EmulationName,LocaleString(MSG_TERMXEM_NO_EMULATION_TXT));
  310.  
  311.         XEmulatorBase    = NULL;
  312.         XEM_Signal    = NULL;
  313.  
  314.         RasterEnabled = TRUE;
  315.  
  316.         ClearCursor();
  317.  
  318.         Reset();
  319.  
  320.         DrawCursor();
  321.     }
  322. }
  323.  
  324.     /* OpenEmulator(STRPTR Name):
  325.      *
  326.      *    Open an emulation library.
  327.      */
  328.  
  329. BYTE
  330. OpenEmulator(STRPTR Name)
  331. {
  332.     CloseEmulator();
  333.  
  334.     XEM_HostData . Source        = NULL;
  335.     XEM_HostData . Destination    = NULL;
  336.     XEM_HostData . InESC        = FALSE;
  337.     XEM_HostData . InCSI        = FALSE;
  338.  
  339.     if(XEmulatorBase = OpenLibrary(Name,0))
  340.     {
  341.         ClearCursor();
  342.  
  343.         Reset();
  344.  
  345.         if(SetupEmulator())
  346.         {
  347.             SetWrMsk(RPort,DepthMask);
  348.  
  349.             if(XEmulatorSetup(XEM_IO))
  350.             {
  351.                 SetEmulatorOptions(XEM_PREFS_LOAD);
  352.  
  353.                 if(XEmulatorOpenConsole(XEM_IO))
  354.                 {
  355.                     STRPTR LibName = FilePart(XEmulatorBase -> lib_Node . ln_Name);
  356.  
  357.                     strcpy(EmulationName,&LibName[3]);
  358.  
  359.                     EmulationName[strlen(EmulationName) - 8] = 0;
  360.  
  361.                     SetupXEM_MacroKeys(MacroKeys);
  362.  
  363.                     return(TRUE);
  364.                 }
  365.             }
  366.         }
  367.  
  368.         DrawCursor();
  369.  
  370.         CloseLibrary(XEmulatorBase);
  371.  
  372.         strcpy(EmulationName,LocaleString(MSG_TERMXEM_NO_EMULATION_TXT));
  373.  
  374.         XEmulatorBase    = NULL;
  375.         XEM_Signal    = NULL;
  376.     }
  377.  
  378.     return(FALSE);
  379. }
  380.  
  381.     /* XOff():
  382.      *
  383.      *    Small local routine, complements XOn() in Serial.c
  384.      */
  385.  
  386. STATIC VOID
  387. XOff(VOID)
  388. {
  389.     if(Status == STATUS_HOLDING)
  390.     {
  391.         UBYTE c = XOF;
  392.  
  393.         SerWrite(&c,1);
  394.  
  395.         Status = STATUS_READY;
  396.     }
  397. }
  398.  
  399.     /* SetupXEM_MacroKeys(struct MacroKeys *Keys):
  400.      *
  401.      *    Sets up the internal representation of the macro key
  402.      *    data to fit the XEM specification.
  403.      */
  404.  
  405. VOID
  406. SetupXEM_MacroKeys(struct MacroKeys *Keys)
  407. {
  408.         /* Are we allowed to do what we want to do? */
  409.  
  410.     if(XEM_MacroKeys && XEmulatorBase && Config -> TerminalConfig -> EmulationMode == EMULATION_EXTERNAL)
  411.     {
  412.         WORD i,j,k = 0;
  413.  
  414.             /* Clear the macro list. */
  415.  
  416.         NewList(&XEM_MacroList);
  417.  
  418.             /* Run down the list of qualifiers. */
  419.  
  420.         for(i = XMKQ_NONE ; i <= XMKQ_CONTROL ; i++)
  421.         {
  422.                 /* Run down the function keys. */
  423.  
  424.             for(j = 0 ; j < 10 ; j++)
  425.             {
  426.                     /* If the key has no data attached,
  427.                      * don't use it in the list.
  428.                      */
  429.  
  430.                 if(Keys -> Keys[i][j][0])
  431.                 {
  432.                     XEM_MacroKeys[k] . xmk_Type        = XMKT_RAWKEY;
  433.                     XEM_MacroKeys[k] . xmk_Qualifier    = i;
  434.                     XEM_MacroKeys[k] . xmk_Code        = 0x50 + j;
  435.                     XEM_MacroKeys[k] . xmk_UserData        = NULL;
  436.  
  437.                     AddTail(&XEM_MacroList,(struct Node *)&XEM_MacroKeys[k++]);
  438.                 }
  439.             }
  440.         }
  441.  
  442.             /* Take care of the rest, add support for the xON key. */
  443.  
  444.         XEM_MacroKeys[k] . xmk_Type         = XMKT_COOKED;
  445.         XEM_MacroKeys[k] . xmk_Qualifier    = NULL;
  446.         XEM_MacroKeys[k] . xmk_Code        = XON;
  447.         XEM_MacroKeys[k] . xmk_UserData        = (APTR)DoxON;
  448.  
  449.         AddTail(&XEM_MacroList,(struct Node *)&XEM_MacroKeys[k++]);
  450.  
  451.             /* Take care of the xOFF key. */
  452.  
  453.         XEM_MacroKeys[k] . xmk_Type         = XMKT_COOKED;
  454.         XEM_MacroKeys[k] . xmk_Qualifier    = NULL;
  455.         XEM_MacroKeys[k] . xmk_Code        = XOF;
  456.         XEM_MacroKeys[k] . xmk_UserData        = (APTR)XOff;
  457.  
  458.         AddTail(&XEM_MacroList,(struct Node *)&XEM_MacroKeys[k]);
  459.  
  460.             /* Make the emulator notice the new settings. */
  461.  
  462.         XEmulatorMacroKeyFilter(XEM_IO,&XEM_MacroList);
  463.     }
  464. }
  465.